package com.hk.yajin.function.videos.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.videos.MenuVideoFragment;
import com.hk.yajin.function.videos.fragment.MySeriseVideoListFragment;
import com.hk.yajin.function.videos.fragment.MySingleVideoListFragment;
import com.hk.yajin.function.videos.fragment.VideoListFragment;
import com.hk.yajin.function.videos.fragment.ViewPorintListFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyVideoActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.rb_one)
    RadioButton rbOne;
    @BindView(R.id.rb_three)
    RadioButton rbThree;
    @BindView(R.id.rb_time)
    RadioGroup rbTime;
    @BindView(R.id.vp_video)
    ViewPager vpVideo;

    private MyViewPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_video);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        vpVideo = (ViewPager) findViewById(R.id.vp_video);
        pagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), getFragments());
        vpVideo.setAdapter(pagerAdapter);
        vpVideo.setOnPageChangeListener(new MyPageChangeListener());
        vpVideo.setOffscreenPageLimit(2);
        vpVideo.setCurrentItem(0);

    }

    private List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new MySingleVideoListFragment());
        fragments.add(new MySeriseVideoListFragment());

        return fragments;
    }

    @OnClick({R.id.rb_one, R.id.rb_three, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rb_one:
                vpVideo.setCurrentItem(0);
                break;
            case R.id.rb_three:
                vpVideo.setCurrentItem(1);
                break;
        }
    }

    class MyViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        /**
         * @param fm
         */
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        public MyViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

    }

    class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    rbOne.setChecked(true);
                    break;
                case 1:
                    rbThree.setChecked(true);
                    break;

            }
        }

    }
}
